package cn.tedu.basic;

import java.io.*;
import java.net.Socket;

// 测试 网络编程 套接字--客户端
public class Client {
    public static void main(String[] args) throws IOException {
        //创建对象--并指定要连接的服务器信息
        Socket socket = new Socket("176.8.3.117", 12345);
        System.out.println("已连接成功");
        //让客户端给服务器发送I love you--out
        OutputStream out = socket.getOutputStream();//获取写出流，写出数据
        out.write("I love you".getBytes());//out是字节流，转为数组
        //读取数据
        InputStream inp = socket.getInputStream();
        for (int i = 0; i < 16; i++) {
            int re = inp.read();
            char cha = (char) re;
            System.out.print(cha);
        }
        inp.close();
        //写出数据--IO自动关闭
        try (OutputStream outp = socket.getOutputStream();) {
            String st = "The day is beautiful !";
            outp.write(st.getBytes());
             out.close();
        } catch (Exception a) {
            System.out.println("123");
        }
        //高级流写出
        BufferedOutputStream bufo =null;
        try {
            bufo = new BufferedOutputStream(socket.getOutputStream());
            String str = "I miss you every day !";
            bufo.write(str.getBytes());
        }catch (Exception a){
            System.out.println("读取失败");
        }finally {
            bufo.close();
        }
        //高级流写入
       try( BufferedInputStream bufin =new BufferedInputStream(socket.getInputStream());){
        for (int i = 0; i <9 ; i++) {
            int read = bufin.read();
            char ch = (char) read;
            System.out.print(ch);
            bufin.close();
        }
        }catch (Exception a){
           System.out.println("1235");
       }
    }
}
